package org.mwielocha.jxcube.core;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.RowData;
import org.mwielocha.jxcube.db.Database;

import static org.mwielocha.jxcube.db.Database.*;

public class SynchDialog extends Dialog {

  protected Object result;
  protected Shell shell;
  
  private List list;
  
  protected ResourceBundle res;

  /**
   * Create the dialog.
   * @param parent
   * @param style
   */
  public SynchDialog(Shell parent, int style) {
    super(parent, style);
    res = ResourceBundle.getBundle("properties/msg");
    setText(res.getString("sychDialog.title"));
  }

  /**
   * Open the dialog.
   * @return the result
   */
  public Object open() {
    createContents();
    load();
    shell.open();
    shell.layout();
    Display display = getParent().getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    return result;
  }

  /**
   * Create contents of the dialog.
   */
  private void createContents() {
    shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
    shell.setLayout(new BorderLayout(0, 0));
    shell.setSize(450, 300);
    shell.setText(getText());
    {
      Composite composite = new Composite(shell, SWT.NONE);
      composite.setLayout(new RowLayout(SWT.VERTICAL));
      composite.setLayoutData(BorderLayout.WEST);
      {
        Button addButton = new Button(composite, SWT.NONE);
        addButton.setLayoutData(new RowData(80, SWT.DEFAULT));
        addButton.setText(res.getString("general.add"));
        addButton.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            DirectoryDialog dd = new DirectoryDialog(shell);
            String path = dd.open();
            if(path != null) {
              list.add(path);
            }
          }
        });
      }
      {
        Button delButton = new Button(composite, SWT.NONE);
        delButton.setLayoutData(new RowData(80, SWT.DEFAULT));
        delButton.setText(res.getString("general.delete"));
        delButton.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            int[] indices = list.getSelectionIndices();
            if (indices.length > 0) {
              list.remove(indices);
            }
          }
        });
      }
      {
        Button okButton = new Button(composite, SWT.NONE);
        okButton.setLayoutData(new RowData(80, SWT.DEFAULT));
        okButton.setText(res.getString("general.ok"));
        okButton.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            shell.setVisible(false);
            save();
          }
        });
      }
      {
        Button cancelButton = new Button(composite, SWT.NONE);
        cancelButton.setLayoutData(new RowData(80, SWT.DEFAULT));
        cancelButton.setText(res.getString("general.cancel"));
        cancelButton.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            shell.setVisible(false);
          }
        });
      }
    }
    {
      list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
      list.setLayoutData(BorderLayout.CENTER);
    }

  }
  
protected void load() {
    
    list.removeAll();
    
    try {

      Database db = new Database();

      try {

        db.open();
        ResultSet rs = db.executeSQL(lookupAllQuery("synch"));

        while (rs.next()) {
          list.add(rs.getString(1));
        }
      } finally {
        db.close();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  protected void save() {
    try {

      Database db = new Database();

      try {

        db.open();
        db.executeDML(clearAllQuery("synch"));

        for (String s : list.getItems()) {
          Map<String, Object> values = new HashMap<String, Object>();
          values.put("path", s);
          db.executeDML(insertQuery("synch", values));
        }
      } finally {

        db.close();

      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
